<template>
  <el-container style="flex-direction: column; width: 640px">
    <!--    <search></search>-->
    <!--搜索相关-->
    <div class="searchClass" style="margin-top: 20px">
      <el-input placeholder="请输入内容" v-model="input" class="input-with-select">
        <el-select v-model="select" slot="prepend" placeholder="请选择" style="width: 100px">
        <!-- 选择相关标签 -->
          <el-option label="全部" aria-selected="true"></el-option>
          <el-option v-for="item in tagList" :key="item.id" :label="item.tagName" :value="item.id">
            {{item.tagName}}
          </el-option>
        </el-select>
        <el-button slot="append" icon="el-icon-search" @keyup.enter="keyDown"
                   @click="searchArticles(select, input)"></el-button>
      </el-input>
    </div>
    <el-main class="me-articles">
      <scroll-page :loading="loading" :offset="offset" :no-data="noData" v-on:load="load">
        <div v-if="articles.length > 0">
          <article-item v-for="a in articles" :key="a.id" v-bind="a"></article-item>
        </div>
        <!-- 如果用户搜索到文章则显示暂无数据 -->
        <div v-else class="no-data-class">暂无数据</div>
      </scroll-page>

    </el-main>
  </el-container>

</template>

<script>
import ArticleItem from '@/components/article/ArticleItem'
import ScrollPage from '@/components/scrollpage'
import {getArticles} from '@/api/article'
import {getAllTags} from '@/api/tag'

export default {
  name: "ArticleScrollPage",
  props: {
    offset: {
      type: Number,
      default: 100
    },
    page: {
      type: Object,
      default() {
        return {}
      }
    },
    query: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  watch: {
    'query': {
      handler() {
        this.noData = false
        this.articles = []
        this.innerPage.pageNumber = 1
        this.getArticles()
      },
      deep: true
    },
    'page': {
      handler() {
        this.noData = false
        this.articles = []
        this.innerPage = this.page
        this.getArticles()
      },
      deep: true
    }
  },
  created() {
    this.getArticles()
    this.getAllTags()
  },
  data() {
    return {
      loading: false,
      noData: false,
      innerPage: {
        pageSize: 20,
        pageNumber: 1,
        name: 'a.createDate',
        sort: 'desc',
        myArticleFlag: 1
      },
      article: {
        query: {
          tagId: '',
          keyword: ''
        }
      },
      articles: [],
      tagList: [],
      input: '',
      select: ''
    }
  },
  mounted () {
    //绑定事件
    window.addEventListener('keydown',this.keyDown);
  },
  //销毁事件
  destroyed(){
    window.removeEventListener('keydown',this.keyDown,false);
  },
  methods: {
    keyDown(e){
      //如果是回车则执行搜索
      if(e.keyCode === 13){
        //需要执行的搜索方法
        let tagId = this.select;
        let keyword = this.input;
        this.searchArticles(tagId, keyword);
      }
    },
    load() {
      this.getArticles()
    },
    view(id) {
      this.$router.push({path: `/view/${id}`})
    },
    getArticles(tagId, keyword) {
      let that = this
      that.loading = true
      that.query.tagId = tagId;
      that.query.keyword = keyword;

      getArticles(that.query, that.innerPage).then(data => {

        let newArticles = data.data
        if (newArticles && newArticles.length > 0) {
          that.innerPage.pageNumber += 1
          that.articles = that.articles.concat(newArticles)
        } else {
          that.noData = true
        }

      }).catch(error => {
        if (error !== 'error') {
          that.$message({type: 'error', message: '文章加载失败!', showClose: true})
        }
      }).finally(() => {
        that.loading = false
      })

    },
    //加载所有的标签，用作查询
    getAllTags() {
      let that = this
      getAllTags().then(data => {
        that.tagList = data.data
      }).catch(error => {
        if (error !== 'error') {
          that.$message({type: 'error', message: '标签加载失败!', showClose: true})
        }

      })
    },
    //搜索出来的结果每次都按照最新结果展示
    searchArticles(tagId, keyword) {
      let that = this
      that.loading = true
      that.query.tagId = tagId;
      that.query.keyword = keyword;
      that.innerPage.pageNumber =1;
      getArticles(that.query, that.innerPage).then(data => {

        let newArticles = data.data
        if (newArticles && newArticles.length > 0) {
          that.articles = newArticles
        } else {
          that.noData = true
          that.articles = '';
        }

      }).catch(error => {
        if (error !== 'error') {
          that.$message({type: 'error', message: '文章加载失败!', showClose: true})
        }
      }).finally(() => {
        that.loading = false
      })
    }
  },
  components: {
    'article-item': ArticleItem,
    'scroll-page': ScrollPage
  }

}
</script>

<style scoped>
.el-card {
  border-radius: 0;
}

.el-card:not(:first-child) {
  margin-top: 10px;

}

.searchClass {
  width: 640px;
  margin-bottom: 10px;
}

.el-select .el-input {
  width: 130px;
}
.input-with-select {
  background-color: #fff;
}
.no-data-class{
  margin-top: 50px;
  margin-left: 250px;
}
</style>
